package com.huluobo.day_1_lifecycledemo.lifecycle;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.huluobo.day_1_lifecycledemo.R;

public class TipDialog extends Dialog implements LifecycleObserver {
    public TipDialog(@NonNull Context context) {
        super(context);
    }
//    public TipDialog(@NonNull Context context) {
//        super(context);
//        if (context instanceof AppCompatActivity) {
//            ((AppCompatActivity) context).getLifecycle().addObserver(this);//直接获取activity中来添加订阅关系
//        }
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    //通过lifecycle 感知activity中销毁的回调,在其中调用dialog消失的方法
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void dismiss() {
        if (isShowing()) {
            dismiss();
        }
    }
}
